package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.comment.CommentService;
import com.qdxwx.models.comment.Comment;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前台评论模块
 *
 * @author developer
 */
@Controller
@RequestMapping("/ajax/comment")
public class AjaxCommentController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AjaxCommentController.class);
    @Autowired
    private CommentService commentService;

    /**
     * 查询评论
     */
    @RequestMapping("/list.json")
    @ResponseBody
    public AjaxResult commentList(HttpServletRequest request) {
        User user = LoginUser.getUser(request);
        PageRequest page = new PageRequest();
        page.setCurrentPage(TKConvert.toInteger(request.getParameter("page")));
        page.setPageSize(TKConvert.toInteger(request.getParameter("pageSize"), 10));
        //查询评论一级
        Comment comment = new Comment();
        comment.setParentId(TKConvert.toInteger(request.getParameter("parentId")));
        comment.setOtherId(TKConvert.toInteger(request.getParameter("otherId")));
        comment.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
        comment.setStatus(TKConvert.toInteger(request.getParameter("status"),null));
        comment.setUserId(user != null ? user.getUserId() : 0);
        String orderBy = request.getParameter("order");
        if ("praiseCount".equals(orderBy)) {
            page.setOrderBy("praise_count desc");
        }
        List<Comment> commentList = commentService.getList(comment, page);// 查询评论
        for (Comment item : commentList) {
            if (item.getParentId() > 0) {
                item.setParent(commentService.get(item.getParentId()));
            }
        }
        return AjaxResult.success(commentList);
    }

    @RequestMapping("/count.json")
    @ResponseBody
    public AjaxResult commentCount(HttpServletRequest request) {
        Comment comment = new Comment();
        comment.setParentId(TKConvert.toInteger(request.getParameter("parentId")));
        comment.setOtherId(TKConvert.toInteger(request.getParameter("otherId")));
        comment.setTypeId(TKConvert.toInteger(request.getParameter("typeId")));
        int count = commentService.count(comment);// 查询评论
        return AjaxResult.success(count);
    }

    @PostMapping("/praise.json")
    @ResponseBody
    public AjaxResult praiseComment(HttpServletRequest request) {
        //User user = LoginUser.getUser(request);
        //if (user == null) {
        //    return AjaxResult.auth("您未登录，请登录后再发表评论！");
        //}
        int commentId = TKConvert.toInteger(request.getParameter("id"));
        commentService.updatePraiseCount(commentId, 1);
        return AjaxResult.success();
    }
}